﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using CrystalDecisions.CrystalReports.Engine;

namespace sales.forms
{
    public partial class voucherCashPayment : voucherCashBase
    {
        public voucherCashPayment()
        {
            try
            {
                InitializeComponent();
                dbcrType = debitCredit.debit;
                int mask = common.SysLibs.TurnBitOn(0, CommonTypes.Consts.constFlagPos_VoucherDetailCatCode_PAYMENT);
                (myDetailGrid as sales.Controls.gridVoucherDetail).LoadCatCode1(mask);
            }
            catch (Exception er)
            {
                ShowError(er);
            }
        }
        #region override function
        private myReports.sales.printCashVoucher4 _myCashVoucherReport = null;
        private myReports.sales.printCashVoucher4 myCashVoucherReport
        {
            get
            {
                if (_myCashVoucherReport == null) _myCashVoucherReport = new myReports.sales.printCashVoucher4();
                return _myCashVoucherReport;
            }
        }

        private void PrintCashVoucher(bool fPreview)
        {
            bool useForeignCurrency = (this.myCurrency != CommonTypes.Settings.sysMainCurrency);

            //Report parametters
            decimal total = (useForeignCurrency ? fSubTotalEd.Value : subTotalEd.Value);
            DateTime dt = this.dateEd.myDateTime; 
            string tmp = application.ReportLibs.SayDate(dt);

            string[] reportTitle = application.ReportLibs.CompanyHeader(cbCompany.myValue);
            string[] workerName = new string[] { CommonTypes.Settings.sysLoginInfo.FullName, note2Ed.Text.Trim() };
            string[] reportData = new string[]{ cbCompany.Text,editKeyEd.Text.Trim(),this.TitleLbl.Text,"",
                                                common.SysLibs.SayNumber((long)total,",","đồng"),
                                                application.ReportLibs.SayDate(dt),note1Ed.Text.Trim() 
                                            };
            try
            {
                string custCode = "";
                data.masterDS.voucherDetailCatCodeRow voucherDetailCatCodeRow;
                myReports.sales.reportDataSet.cashDetailTblRow rowDetail;
                myReports.sales.reportDataSet.cashDetailTblDataTable myReportTbl = new myReports.sales.reportDataSet.cashDetailTblDataTable();
                myReportTbl.Clear();

                data.masterDS.CustomerRow custRow;
                for (int idx = 0; idx < myApplicationDS.VoucherDetail.Count; idx++)
                {
                    rowDetail = myReportTbl.NewcashDetailTblRow();
                    if (!myApplicationDS.VoucherDetail[idx].IsrefVoucherNo1Null())
                        rowDetail.voucherNo = myApplicationDS.VoucherDetail[idx].refVoucherNo1;
                    if (!myApplicationDS.VoucherDetail[idx].IsrefVoucherDate1Null())
                        rowDetail.ondate = myApplicationDS.VoucherDetail[idx].refVoucherDate1;

                    rowDetail.saleCode = myApplicationDS.VoucherDetail[idx].saleCode;
                    if (!myApplicationDS.VoucherDetail[idx].IscustCodeNull())
                        rowDetail.custCode = myApplicationDS.VoucherDetail[idx].custCode;
                    if (useForeignCurrency)
                        rowDetail.amount1 = myApplicationDS.VoucherDetail[idx].fAmount;
                    else rowDetail.amount1 = myApplicationDS.VoucherDetail[idx].amount;
                    rowDetail.amount2 = 0;

                    rowDetail.notes = "";
                    if (!myApplicationDS.VoucherDetail[idx].IsnotesNull() && myApplicationDS.VoucherDetail[idx].notes.Trim() != "")
                        rowDetail.notes += (rowDetail.notes == "" ? "" : "-") + myApplicationDS.VoucherDetail[idx].notes;
                    else
                    {
                        if (!myApplicationDS.VoucherDetail[idx].IscatCode1Null())
                        {
                            voucherDetailCatCodeRow = DataAccess.DataCache.FindVoucherDetailCat(myApplicationDS.VoucherDetail[idx].catCode1);
                            if (voucherDetailCatCodeRow != null)
                                rowDetail.notes += (rowDetail.notes == "" ? "" : "-") + voucherDetailCatCodeRow.description;
                        }
                    }
                    //Customer name
                    if (!rowDetail.IscustCodeNull())
                    {
                        custCode = rowDetail.custCode.Trim();
                        custRow = DataAccess.DataCache.FindCUSTOMER(custCode);
                        if (custRow != null) rowDetail.custName = custRow.name1;
                    }
                    myReportTbl.AddcashDetailTblRow(rowDetail);
                }
                byte decimalPlace = (useForeignCurrency ? common.SysLibs.GetPrecisionFromMask(CommonTypes.Settings.sysMaskForeignAmt) : common.SysLibs.GetPrecisionFromMask(CommonTypes.Settings.sysMaskLocalAmt));
                string[] fldList = new string[] { "amount1", "sumOfAmount1"};
                application.ReportLibs.SetReportDecimalPlace(myCashVoucherReport, fldList, decimalPlace);

                myCashVoucherReport.SetDataSource(myReportTbl.DefaultView);
                this.SetReport(myCashVoucherReport);
                this.SetReport(workerName, "workerName");
                this.SetReport(reportTitle, "reportTitle");
                this.SetReport(reportData, "reportData");
                this.SetReport(application.ReportLibs.GetCompanyLogoFile(cbCompany.myValue), "logoPath");
                this.ShowReport();
            }
            catch (Exception er)
            {
                this.ShowError(er);
            }
            return;
        }
        protected override void SetDetailGrid()
        {
            base.SetDetailGrid();
            this.myDetailGrid.SetColumnVisible(new string[] {
                                                sales.Controls.gridVoucherDetail.ColumnNames.saleCode.ToString(), 
                                                sales.Controls.gridVoucherDetail.ColumnNames.custCode.ToString(), 
                                                sales.Controls.gridVoucherDetail.ColumnNames.fAmount.ToString(),
                                                sales.Controls.gridVoucherDetail.ColumnNames.amount.ToString(),
                                                sales.Controls.gridVoucherDetail.ColumnNames.refVoucherNo1.ToString(),
                                                sales.Controls.gridVoucherDetail.ColumnNames.refVoucher1Date.ToString(),
                                                sales.Controls.gridVoucherDetail.ColumnNames.catCode1.ToString(),
                                                sales.Controls.gridVoucherDetail.ColumnNames.notes.ToString()});
        }
        public override void Print(bool fPreviewBeforePrint)
        {
            PrintCashVoucher(fPreviewBeforePrint);
        }
        #endregion
        
    }
}
